package com.cdpowernode.pncrm.service.impl;

import com.cdpowernode.pncrm.exception.CrmException;
import com.cdpowernode.pncrm.javabean.Activity;
import com.cdpowernode.pncrm.javabean.ActivityRemark;
import com.cdpowernode.pncrm.javabean.ActivityVo;
import com.cdpowernode.pncrm.mapper.ActivityMapper;
import com.cdpowernode.pncrm.mapper.ActivityRemarkMapper;
import com.cdpowernode.pncrm.service.ActivityService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author jy
 * @version 1.0
 * @date 2021/12/6 13:05
 */
@Service
public class ActivityServiceImpl implements ActivityService {

    @Autowired
    private ActivityMapper activityMapper;
    @Autowired
    private ActivityRemarkMapper activityRemarkMapper;

    @Override
    public void addActivity(Activity activity) {
        int result = activityMapper.insertActivity(activity);
        if (result != 1){
            throw new CrmException("添加市场活动失败!");
        }
    }

    /**
     * 按指定条件使用分页查询
     * @param activityVo 封装了查询条件和分页的数据
     * @return 包含了所有页面信息的PageInfo对象
     */
    @Override
    public PageInfo<Activity> findByCondition(ActivityVo activityVo) {
        Integer pageNum = activityVo.getPageNum();
        Integer pageSize = activityVo.getPageSize();
        PageHelper.startPage(pageNum,pageSize);
        List<Activity> activityList = activityMapper.findByCondition(activityVo);
        return new PageInfo<>(activityList);
    }

    @Override
    public Activity findById(String id) {
        return activityMapper.findById(id);
    }

    @Override
    public List<Activity> findXzById(String[] ids) {
        return activityMapper.findXzById(ids);
    }

    @Override
    public void updateActivity(Activity activity) {
        int result = activityMapper.updateActivity(activity);
        if (result != 1){
            throw new CrmException("更新失败");
        }
    }

    @Override
    public boolean deleteActivityById(String[] ids) {
        boolean flag = true;

        //查询出需要删除的备注的数量
        int count1 = activityRemarkMapper.getCountByAids(ids);

        //删除备注，返回受到影响的条数(实际删除的数量)
        int count2 = activityRemarkMapper.deleteByAids(ids);
        if (count1 != count2){
            flag = false;
        }

        //删除市场活动
        int count3 = activityMapper.deleteActivityById(ids);
        if (count3 != ids.length){
            flag = false;
        }

        return flag;

    }

    //查询所有的市场活动
    @Override
    public List<Activity> findAll() {
        return activityMapper.findAll();
    }

    //向数据库中添加多条市场活动数据
    @Override
    public void addActivityList(List<Activity> activityList) {
        for (Activity activity : activityList) {
            addActivity(activity);
        }
    }

    @Override
    public List<ActivityRemark> getRemarkListByAid(String id) {
        return activityRemarkMapper.getRemarkListByAid(id);
    }

    @Override
    public boolean deleteRemark(String id) {

        boolean flag = true;

        int count = activityRemarkMapper.deleteRemarkById(id);
        if (count != 1){
            flag = false;
        }
        return flag;
    }

    @Override
    public boolean saveRemark(ActivityRemark ar) {
        boolean flag = true;

        int count = activityRemarkMapper.saveRemarkById(ar);
        if (count != 1){
            flag = false;
        }
        return flag;
    }
}
